<?php
namespace V1\Controller;
use Common\Library\Exception\BadRequest;
/**
 * 新闻
 */
class NewsController extends BaseController {
    
    protected $errorCode = array(
        'index' => array(
            '-10'=>'数据木有啦',
        ) ,
        'bookmark' => array(
            '-1'=>'用户token失效或有误',
            '-10'=>'添加不成功',
            '-11'=>'已添加成功，请勿重复操作',
        ),
    );
    /**
     * 
     * 股票新闻列表 
     */
    public function index(){
        
        $data = array("code" => 0,"msg" => "","info"=>array());

        
        $pageSize = I('r',10,'intval');
        $pageIndex = I('p',1,'intval');
        
        $pageIndex>10 && $this->responseError(-10);
        
//        $today=  date('Y-m-d');
//        $result = M('MatiIndex')->where($where)->field('stock_code')->order('stock_order desc')->limit(20)->cache("matiindex{$today}",3600)->select();
//        $stockList=[];
//        if(!empty($result)){
//            foreach ($result as $key => $value) {
//                $stockList[]=$value['stock_code'];
//            }
//        }
//   
//        $where = array('stockcode'=> array('in',$stockList));
        $orders =array('published'=>'desc');

        
        $fields=['newsid','positive','negative','published'];
        
        $newList = M('NewsTag','stock_',C('DB_MT_CORE'))->where($where)->field($fields)->order($orders)->page($pageIndex,$pageSize)->cache(true,3600)->select();
        $resultNewsList =[];
        $newsModel =  M('News','stock_',C('DB_MT_CORE'));
        $fields=['title','fragment','url'];
        foreach ($newList as $key => $value) {
            
            $where=array('state'=>1,'id'=>$value['newsid'],'title'=>array('notlike',array('%å%','%æ%'),'OR'));
            $result = $newsModel->where($where)->field($fields)->cache(true,3600)->find();
            if(empty($result))                continue;
            $value['positive']=  round($value['positive'], 2);
            $value['negative']=  round($value['negative'], 2);
            $resultNewsList[] = array_merge($result,$value);
        }
        unset($newList);
        $data['info'] = $resultNewsList;
        $this->responseResult($data);
    }

    /**
     * 用户收藏话题
     */
    public function bookmark(){
      $data = array("code" => 0,"msg" => "","info"=>array());
      
      $topic_id = I('id',0);
      
      if(empty($topic_id)){
            throw new BadRequest("参数异常", 1);
       }
       
      list($uid,$user_token) = $this->checkUserToken();
      
      
      $userTopicModel = M('UserTopic');
      
      $count = $userTopicModel->where(array('user_id' => $uid,'topic_id' => $topic_id,'status'=>1))->count();
      !empty($count) && $this->responseError(-11);
      
      $map = ['user_id' => $uid,'topic_id' => $topic_id,'create_time' => NOW_TIME,'update_time' => NOW_TIME,];
      if($userTopicModel->create($map)){
            $userTopicModel->add();
            M('Topic')->where(array('topic_id' => $topic_id))->setInc('bookmark',1);
            $data['msg']='添加成功';
            $this->responseResult($data);
      } else {
            $this->responseError(-10);
      }
    }
    
}